# -*- coding: utf-8 -*-
import re
import maya.cmds as mc
from liberHook.BaseHook import BaseHook
from liberLibs.dccLibs import Maya
reload(Maya)


class Hook(BaseHook):
    def __init__(self, library, directory, ext, start, end):
        BaseHook.__init__(self, library, directory, ext, start, end)

    def execute(self):
        if not Maya.load_plugin("redshift4maya.mll"):
            self.append_error("Load redshift4maya.mll failed.")
            return
        if not self.files:
            self.append_error("No vdb files found")
            return
        file_num = len(self.files)

        file_name = self.files[0]
        node = Maya.import_rs_vdb(file_name)
        if file_num > 1:
            mc.setAttr("%s.ufe" % node, 1)
            exp = mc.createNode("expression", name="expression")
            mc.connectAttr("%s.out[0]" % exp, "%s.frameExtension" % node, f=1)
            mc.expression(exp, e=1, s="{0}.frameExtension=frame".format(node), ae=1, uc="all")
        # assign shader
        sg = Maya.create_rs_volume()
        mc.sets(node, fe=sg)

